package com.health.viewscontrols.print;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.health.viewscontrols.NovaApplication;
import com.health.db.FDbManager;
import com.health.model.PrintHealthReport;
import com.health.tablebean.ARCHIVE_ASSISTCHECK;
import com.health.tablebean.ARCHIVE_GENERALCONDITION;
import com.health.tools.GlobalConts;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;

public class PrintDataAction implements OnClickListener {
	private Context context = null;
	private String deviceAddress = null;
	private PrintDataService printDataService = null;

	public PrintDataAction(Context context, String deviceAddress) {
		super();
		this.context = context;
		this.deviceAddress = deviceAddress;
		this.printDataService = new PrintDataService(this.context, this.deviceAddress);
		this.initView();

	}

	@SuppressWarnings("static-access")
	public void disconnect() {
		try {
			printDataService.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initView() {
		// 设置当前设备名称
		// 一上来就先连接蓝牙设备
		boolean flag = this.printDataService.connect();
		if (flag == false) {
			// 连接失败
			// this.connectState.setText("连接失败！");
			System.out.println("连接失败!");
		} else {
			// 连接成功
			// this.connectState.setText("连接成功！");
			// syso
		}
	}

	// public void setPrintData(EditText printData) {
	// this.printData = printData;
	// }

	@Override
	public void onClick(View v) {
		// this.printDataService.send(sendData + "\n");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String strDate = sdf.format(new Date());
		PrintHealthReport phr = new PrintHealthReport();
		phr.setName(NovaApplication.getUserInfo().getCUSTOMERNAME());
		phr.setAge(GlobalConts.getAge(NovaApplication.getUserInfo().getBIRTHDAY()));
		phr.setDate(strDate);

		List<ARCHIVE_GENERALCONDITION> ags = FDbManager.getInstance(context).findAllByWhere(
				ARCHIVE_GENERALCONDITION.class, "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'");
		if (ags.size() > 0) {
			ARCHIVE_GENERALCONDITION ag = ags.get(ags.size() - 1);
			phr.setHeight(ag.getHEIGHT());
			phr.setWeight(ag.getWEIGHT());
			try {
				phr.setBMI(GlobalConts.getBMI(Double.parseDouble(ag.getHEIGHT()), Double.parseDouble(ag.getWEIGHT()))
						+ "");
			} catch (Exception e) {
				e.printStackTrace();
			}
			phr.setSsy(ag.getLEFTHEIGHT());
			phr.setSzy(ag.getLEFTPRE());
			// phr.setXueYang(a);
			// phr.setNiaoSuan(niaoSuan);

			/////
			List<ARCHIVE_ASSISTCHECK> assistChecks = FDbManager.getInstance(context).findAllByWhere(
					ARCHIVE_ASSISTCHECK.class, "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'");
			if (assistChecks.size() > 0) {
				ARCHIVE_ASSISTCHECK asck = assistChecks.get(assistChecks.size() - 1);
				phr.setXueTang(asck.getFPGL());
				phr.setZongDanGuChun(asck.getTC());
			}
			/////
			phr.setYaoWei(ag.getWAISTLINE());
			phr.setJkjy("");
		}
		this.printDataService.send(printHealthReports(phr));
	}

	/**
	 * 
	 * @param phr
	 * @return
	 * @author 鹿云龙
	 * @date 2016年1月14日 上午8:59:43
	 */
	private static String printHealthReports(PrintHealthReport phr) {
		// 检查项目 检查结果 参考标准
		// 检查项目 检查结果 参考标准
		String str = "" + "用户: " + phr.getName() + "\r\n" + ////
				"性别: 男                  年龄: 12" + "\r\n" + ////
				"检测日期: " + phr.getDate() + "\r\n" + ////
				"- - - - - - - - - - - - - - - - - - - - - -" + "\r\n" + ////
				"- - - - - - - - - - - - - - - - - - - - - -" + "\r\n" + ////
				"检查项目	检查结果	参考标准" + "\r\n" + ////
				"身高		" + phr.getHeight() + "		CM" + "\r\n" + ////
				"体重		" + phr.getWeight() + "		69.8-85.4KG" + "\r\n" + ////
				"BMI		" + phr.getBMI() + "		" + "18.5-23.9kg/m2" + "\r\n" + ////
				"收缩压		" + phr.getSsy() + "		" + "90-139mmhg" + "\r\n" + ////
				"舒张压		" + phr.getSzy() + "		" + "60-89mmhg" + "\r\n" + ////
				"血氧饱和度	" + phr.getXueYang() + "		" + "95%-100%" + "\r\n" + ////////
				"血糖(空腹)	" + phr.getXueTang() + "		" + "3.9-6.1mmol/L" + "\r\n" + ////
				"尿酸		" + phr.getNiaoSuan() + "	     " + "0.149--0.416mmol/L" + "\r\n" + ////
				"总胆固醇	" + phr.getZongDanGuChun() + "		" + "<5.18mmol/L" + "\r\n" + ////
				"腰围		" + phr.getYaoWei() + "		" + "----" + "\r\n" + "\r\n" + ////
				"健康建议:" + phr.getJkjy() + "\r\n"////
				+ "- - - - - - - - - - - - - - - - - - - - - -";

		return str;
	}
}
